package org.ank.food;

import android.content.Context;
import android.database.Cursor;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class AdviceRequestHandler extends Handler{
	
	public static final int WAIT = 0;
	public static final int READY = 1;
	
	private TextView indicator;
	private SimpleCursorAdapter adapter;
	private ListView listView;
//	private Button button;
	private ProgressBar progressBar;
//	private int waitString;
	private Context context;
	private Button button1,button2;
	
	public AdviceRequestHandler(ProgressBar b, TextView t, ListView l, SimpleCursorAdapter a, Context c,
			Button b1, Button b2){
		this.indicator = t;
		this.progressBar = b;
		this.listView = l;
		this.adapter = a;
		this.context = c;
		this.button1 = b1;
		this.button2 = b2;
	}
	
	@Override
	public void handleMessage(Message m){
		switch (m.what){
		case WAIT :
			progressBar.setVisibility(ProgressBar.VISIBLE);
			indicator.setVisibility(TextView.VISIBLE);
			listView.setVisibility(ListView.INVISIBLE);
			button1.setEnabled(false);
			button2.setEnabled(false);
//			listView.setAdapter(null);
			break;
		case READY :
			progressBar.setVisibility(ProgressBar.INVISIBLE);
			indicator.setVisibility(TextView.INVISIBLE);
//			listView.setAdapter(adapter);
			button1.setEnabled(true);
			button2.setEnabled(true);
			Cursor cursor = AdviceManager.getInstance(context).getLastAdviceCursor();
			adapter.changeCursor(cursor);
			listView.setVisibility(ListView.VISIBLE);
			
			break;
		}
	}
	
//	private ArrayAdapter<String> getAdapter(AdviceRequestHandler h){
//		final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),
//				R.id.profile_list_item_text);
//		final AdviceRequestHandler handler = h;
//		Thread t = new Thread(new Runnable(){
//			@Override
//			public void run() {
//				Message m = Message.obtain(handler, AdviceRequestHandler.WAIT);
//				m.sendToTarget();
//				String[] strings = AdviceManager.getInstance(getApplicationContext()).getAdvice();
//				for(int i=0;i<strings.length;i++){
//					adapter.add(strings[i]);
//				}
//				m = Message.obtain(handler, AdviceRequestHandler.READY);
//				m.sendToTarget();
//			}
//		});
//		t.start();
//		return adapter;
//	}
}